package in.muton.edushare.components.messaging.api;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.roles.CourseRoleCheck;
import in.muton.edushare.auth.roles.DeptRoleCheck;
import in.muton.edushare.auth.roles.UnivRoleCheck;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.MessagesDAO;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class MessageModel extends CayenneList<Messages> {
	@Override
	public List<IModel<Messages>> getObject() {
		if (reciever2 == null || reciever1.getObject() == reciever2.getObject())
			return getGroupMessages();
		else
			return getUserMessages();
	}

	public boolean getUpdate() {
		List<Messages> msgl = new MessagesDAO(EduShareSession.get()
				.getSessionORMInterface()).getMessagesReceived(reciever1
				.getObject(), 20, privacyLevel);
		if (msgl.size() > 0) {
			if (msgl.get(0).getMessageId() != currMsgList.getObject().get(0)
					.getObject().getMessageId())
				return true;
			else
				return false;
		} else
			return false;
	}

	private List<IModel<Messages>> getGroupMessages() {
		if(reciever1.getObject() instanceof Users){
			currMsgList = new CayenneList<Messages>(new MessagesDAO(EduShareSession
					.get().getSessionORMInterface()).getUserMessages((Users)reciever1
							.getObject(), 20));
			return currMsgList.getObject();
		}else{
			currMsgList = new CayenneList<Messages>(new MessagesDAO(EduShareSession
					.get().getSessionORMInterface()).getMessagesReceived(reciever1
					.getObject(), 20, Messages.PRIVACY_TYPE_PUBLIC));
			return currMsgList.getObject();
		}
	}

	private List<IModel<Messages>> getUserMessages() {
		currMsgList = new CayenneList<Messages>(new MessagesDAO(EduShareSession
				.get().getSessionORMInterface()).getUserToUserMessages(
				(Users) reciever1.getObject(), (Users) reciever2.getObject(),
				ApplicationConstants.PAGE_SIZE, Messages.PRIVACY_TYPE_PRIVATE));
		return currMsgList.getObject();
	}

	public MessageModel(Departments reciever, Users user) {
		super(null);
		this.reciever1 = new CayenneModel<Departments>(reciever);
		DeptRoleCheck drc = new DeptRoleCheck(reciever, user);
		if (drc.isPrimaryAdmin()) {
			privacyLevel = Messages.PRIVACY_TYPE_PRIVATE;
		} else if (drc.isSecondaryAdmin() || drc.isTeacher()) {
			privacyLevel = Messages.PRIVACY_TYPE_SHARED;
		} else if (drc.isStudent()
				|| DeptRoleCheck.isUnivMember(reciever, user)) {
			privacyLevel = Messages.PRIVACY_TYPE_PUBLIC;
		}
	}

	public MessageModel(Universities reciever, Users user) {
		super(null);
		this.reciever1 = new CayenneModel<Universities>(reciever);
		UnivRoleCheck urc = new UnivRoleCheck(reciever, user);
		if (urc.isAdmin()) {
			privacyLevel = Messages.PRIVACY_TYPE_PRIVATE;
		} else if (urc.isTeacher()) {
			privacyLevel = Messages.PRIVACY_TYPE_SHARED;
		} else if (urc.isStudent()) {
			privacyLevel = Messages.PRIVACY_TYPE_PUBLIC;
		}
	}

	public MessageModel(CourseInstances reciever, Users user) {
		super(null);
		this.reciever1 = new CayenneModel<CourseInstances>(reciever);
		CourseRoleCheck crc = new CourseRoleCheck(reciever, user);
		if (crc.isTeacher() || crc.isAdmin()) {
			privacyLevel = Messages.PRIVACY_TYPE_PRIVATE;
		} else if (crc.isStudent()) {
			privacyLevel = Messages.PRIVACY_TYPE_SHARED;
		} else if (CourseRoleCheck.isDeptMember(reciever, user)
				|| CourseRoleCheck.isUnivMember(reciever, user)) {
			privacyLevel = Messages.PRIVACY_TYPE_PUBLIC;
		}
	}

	public MessageModel(Users reciever, Users viewer) {
		super(null);
		this.reciever1 = new CayenneModel<Users>(reciever);
		this.reciever2 = new CayenneModel<Users>(viewer);
	}
	
	public MessageModel(Groups receiver, Users viewer){
		super(null);
		this.reciever1 = new CayenneModel<Groups>(receiver);
		if(viewer.isGroupAdmin(receiver)){
			privacyLevel = Messages.PRIVACY_TYPE_PRIVATE;
		}else if(viewer.isGroupMember(receiver)){
			privacyLevel = Messages.PRIVACY_TYPE_SHARED;
		}else{
			privacyLevel = Messages.PRIVACY_TYPE_PUBLIC;
		}
	}

	private int privacyLevel = Messages.PRIVACY_TYPE_NONE;

	private CayenneList<Messages> currMsgList = null;
	private IModel<? extends Object> reciever1 = null;
	private IModel<? extends Object> reciever2 = null;
	private static final long serialVersionUID = -3745878105278651939L;
}
